package com.das.mechanic_base.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import com.das.mechanic_base.app.X3MyApp;
import com.das.mechanic_base.bean.common.LocaleBean;

import java.util.Locale;


/**
 * 作者：zhanglei on 2019-12-05 17:15
 * <p>
 * 邮箱：916256628@qq.com
 */
public class LanguageUtiles {

    public static Context appContext = X3MyApp.getInstance().getApp();

    public static Context changeAppLanguage(Locale locale, boolean persistence,Context mContext) {
        if (mContext == null) {
            return X3MyApp.getInstance().getApp();
        }
        Resources resources = mContext.getResources();
        //获取屏幕参数
        DisplayMetrics metrics = resources.getDisplayMetrics();
        //获取设置对象
        Configuration configuration = resources.getConfiguration();
        //如果API < 17
        if (Build.VERSION.SDK_INT >= 24) {
            configuration.setLocale(locale);
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
            mContext.createConfigurationContext(configuration);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //Android 4.1 以上方法
            configuration.setLocale(locale);
            mContext.createConfigurationContext(configuration);
        } else {
            configuration.locale = locale;
        }
        appContext = mContext;

        //更新配置信息
        Locale.setDefault(locale);
        resources.updateConfiguration(configuration, metrics);
        //持久化
        if (persistence) {
            SpHelper.saveData("language", locale.getLanguage() + "-" + locale.getCountry());
        }
        return appContext;
    }

    /**
     * 更新语言设置
     *
     * @param context
     * @param locale
     * @return
     */
    public static Context updateResources(Context context, Locale locale) {
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration configuration = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= 24) {
            configuration.setLocale(locale);
            configuration.setLocales(new LocaleList(locale));
            context = context.createConfigurationContext(configuration);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //Android 4.1 以上方法
            configuration.setLocale(locale);
            context = context.createConfigurationContext(configuration);
        } else {
            configuration.locale = locale;
        }
        res.updateConfiguration(configuration, res.getDisplayMetrics());
        return context;
    }

    /**
     * 设置语言类型
     */
    public static void setApplicationLanguage(Context mContext) {
        if (mContext == null) {
            return;
        }
        Resources resources = mContext.getApplicationContext().getResources();
        //获取屏幕参数
        DisplayMetrics metrics = resources.getDisplayMetrics();
        //获取设置对象
        Configuration configuration = resources.getConfiguration();
        Locale locale = getAppLocale(mContext.getApplicationContext()).locale;
        //如果API < 17
        if (Build.VERSION.SDK_INT >= 24) {
            configuration.setLocale(locale);
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
            mContext.getApplicationContext().createConfigurationContext(configuration);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //Android 4.1 以上方法
            configuration.setLocale(locale);
            mContext.getApplicationContext().createConfigurationContext(configuration);
        } else {
            configuration.locale = locale;
        }
        appContext = mContext;

        //更新配置信息
        Locale.setDefault(locale);
        resources.updateConfiguration(configuration, metrics);
    }

    /**
     * 获取App当前语言
     * 如果存过信息那么以存的信息为准否则取系统的配置信息为准
     *
     * @Return Locale
     **/
    public static LocaleBean getAppLocale(Context mContext) {
        //读取本地配置文件信息
        LocaleBean localeBean = new LocaleBean();
        Locale currentAppLocale = null;
        String language = (String) SpHelper.getData("language","");
        if (!X3StringUtils.isEmpty(language)) {
            localeBean.isSave = true;
            String[] split = language.split("-");
            if (split != null) {
                if (split.length == 1) {
                    currentAppLocale = new Locale(split[0],"");
                }else if (split.length == 2) {
                    currentAppLocale = new Locale(split[0],split[1]);
                }
            }
        }
        if (currentAppLocale == null){
            currentAppLocale = getSystemLocale();
            localeBean.isSave = false;
        }
        localeBean.locale = currentAppLocale;
        return localeBean;
    }

    /******
     * 获取当前系统语言
     */
    public static Locale getSystemLocale() {
        Locale currentSystemLocale;
        //API >= 24
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            currentSystemLocale = Resources.getSystem().getConfiguration().getLocales().get(0);
        } else {
            currentSystemLocale = Resources.getSystem().getConfiguration().locale;
        }
        return currentSystemLocale;
    }

    /**
     * 判断是否是简体中文
     */
    public static boolean isZhRCN() {
        //"zh", "CN"
        LocaleBean systemLocale = getAppLocale(X3MyApp.getInstance().getApp());
//        Log.e("SSSS","获取是否当前的语言:" + systemLocale.locale.getLanguage());
        if (systemLocale.locale.getLanguage().equalsIgnoreCase("zh")) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否是德文
     */
    public static boolean isDe() {
        //"zh", "CN"
        LocaleBean systemLocale = getAppLocale(X3MyApp.getInstance().getApp());
        if (systemLocale.locale.getLanguage().equalsIgnoreCase("de")) {
            return true;
        }
        return false;
    }
}